<template>
  <div class="app-container">
    <div style="width: 800px; margin: 0 auto">
      <el-card>
        <div slot="header"></div>
        <div>
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="最低利润" prop="minProfitRate">
              <el-input
                v-model="form.minProfitRate"
                placeholder="请输入最低利润"
              >
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
            <el-form-item label="最大利润" prop="maxProfitRate">
              <el-input
                v-model="form.maxProfitRate"
                placeholder="请输入最大利润"
              >
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
            <el-form-item label="最小利润额" prop="minProfitAmount">
              <el-input
                v-model="form.minProfitAmount"
                placeholder="请输入最小利润额"
              >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>
            <el-form-item label="最大利润额" prop="maxProfitAmount">
              <el-input
                v-model="form.maxProfitAmount"
                placeholder="请输入最大利润额"
              >
                <template slot="append">元</template>
              </el-input>
            </el-form-item>

            <el-form-item label="是否启用" prop="isActive">
              <el-radio-group v-model="form.isActive">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="" style="text-align: center">
              <el-button type="primary" :loading="loading" @click="submitForm"
                >保存设置</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { queryPro, addPro } from "@/api/newGoods/profitRules";
export default {
  name: "ProfitRules",
  data() {
    return {
      // 遮罩层
      loading: false,

      // 表单参数
      form: {
        isActive: 1,
      },
      // 表单校验
      rules: {
        minProfitRate: [
          { required: true, message: "最低利润不能为空", trigger: "blur" },
        ],
        maxProfitRate: [
          { required: true, message: "最大利润不能为空", trigger: "blur" },
        ],
        minProfitAmount: [
          { required: true, message: "最小利润额不能为空", trigger: "blur" },
        ],
        maxProfitAmount: [
          { required: true, message: "最大利润额不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getRules();
  },
  methods: {
    getRules() {
      queryPro().then((res) => {
        this.form = {
          ...res.data,
        };
      });
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.loading = true;
          addPro(this.form)
            .then((response) => {
              this.$modal.msgSuccess("提交成功");
              this.loading = false;
            })
            .catch((err) => {
              this.loading = false;
            });
        }
      });
    },
  },
};
</script>
